package bg.bgfirst;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.telephony.TelephonyManager;
import android.util.Log;

public class IncomingCallReciever extends BroadcastReceiver {

	public final static String KEY_MESSAGE_ALERT_CALL = "bg.bgfirst.AlertCall";
	public final static String KEY_NUMERO_CALLER_ = "bg.bgfirst.NumeroCaller";
	private ApplicationBg applicationBg;
	
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Context applicationContext = context.getApplicationContext();
		applicationBg = (ApplicationBg) applicationContext;

		Log.v("BG", "intent.getAction  " + intent.getAction());
		if ("android.intent.action.PHONE_STATE".equals(intent.getAction())) {
			Log.v("BG", "BINGO !!!! hashcode : " + this.hashCode()+ "  | applicationBg.hashCode " + applicationBg.hashCode());
			String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
			Log.v("BG", "BINGO !!!! state : " + state + " | Z_1 : " + applicationBg.getRingerMode_Z_1());
			if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
				String caller = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
				if (caller == null){
					caller = ApplicationBg.BG_UNKNOWN_;
				}
				Log.v("BG", "RINGG  caller : " + caller);
				Log.v("BG", "RINGG  isCallerBlackListed : " + applicationBg.isBlackListed(caller));
				boolean blackListed = applicationBg.isBlackListed(caller);
				showAlertDialog_(context, "callBg " + caller+" in BlackList :"+blackListed,caller);
				if (blackListed){
					setSilence(context);
				}
			}
			if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)){
				Log.v("BG", "IDLE applicationBg.isModeSilent  "+applicationBg.isModeSilent());
				if (applicationBg.isModeSilent()){
					setSilenceZ_1_(context);
				}
			}
		}
	}

	

	private void setSilence(Context context) {
		AudioManager aM = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		applicationBg.setRingerMode_Z_1(aM.getRingerMode());
		applicationBg.setModeSilent(true);
		aM.setRingerMode(AudioManager.RINGER_MODE_SILENT);
	}
	
	private void setSilenceZ_1_(Context context) {
		Log.i("BG","setSilenceZ_1 getRingerMode_Z_1 :"+applicationBg.getRingerMode_Z_1());
		AudioManager aM = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		applicationBg.setModeSilent(false);
		aM.setRingerMode(applicationBg.getRingerMode_Z_1());
		Log.i("BG", "setSilenceZ_1 "+applicationBg.getRingerMode_Z_1());
	}


	public static void showAlertDialog_(Context context, String message, String numero) {
		Log.v("BG", "showAlertDialog ");
		Intent intent = new Intent(context, ActivityAlertPhone.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(KEY_MESSAGE_ALERT_CALL, message);
		
		intent.putExtra(KEY_NUMERO_CALLER_, numero);
		Log.v("Bg", "startAlertDialog");
		context.startActivity(intent);
	}
	
	
}